package swing;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.Random;
import java.util.Vector;

/**
 * 表格行管理演示
 * @author fidel
 */
public class TableRowManageDemo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame form = new JFrame("表格行管理");
            form.setVisible(true);
            form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            form.setSize(600, 400);
            form.setLocationRelativeTo(null);
            form.setLayout(new BorderLayout());
            // 构造一个不包含任何单元格数据且由有序列名集合构成的表格模型
            TableModel model = new DefaultTableModel(null, columnNames());
            JTable table = new JTable(model);
            JScrollPane scrollPane = new JScrollPane(table);
            form.getContentPane().add(scrollPane);
            JPanel buttonPanel = new JPanel();
            form.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            JButton button1 = new JButton("添加一行");
            buttonPanel.add(button1);
            button1.addActionListener(e -> {
                ((DefaultTableModel) model).addRow(dataValue());
                table.setModel(model);
            });
            JButton button2 = new JButton("移除最后一行");
            buttonPanel.add(button2);
            button2.addActionListener(e -> {
                // 如果列表模型中的行数大于0
                if (model.getRowCount() > 0) {
                    // 移除表格的最后一行
                    ((DefaultTableModel) model).removeRow(model.getRowCount() - 1);
                }
            });
            JButton button3 = new JButton("移除选中的行");
            buttonPanel.add(button3);
            button3.addActionListener(e -> {
                // 如果选中了表格中一行以上的记录
                if (table.getSelectedRowCount() > 0) {
                    // 获取选择的行的索引数组
                    int[] array = table.getSelectedRows();
                    // 从索引数组的最后一个索引开始移除表格行，确保数组中靠前的索引值不变
                    for (int i = array.length - 1; i >= 0; i--) {
                        ((DefaultTableModel) model).removeRow(array[i]);
                    }
                }
            });
            JButton button4 = new JButton("移除所有行");
            buttonPanel.add(button4);
            button4.addActionListener(e -> {
                if (model.getRowCount() > 0) {
                    for (int i = model.getRowCount() - 1; i >= 0; i--) {
                        ((DefaultTableModel) model).removeRow(i);
                    }
                }
            });
        });
    }

    private static Vector<Object> dataValue() {
        Random random = new Random();
        Vector<Object> dataValue = new Vector<>();
        dataValue.addElement(flag());
        if (flag()) {
            dataValue.addElement(random.nextInt(Byte.MAX_VALUE));
            dataValue.addElement(random.nextInt(Short.MAX_VALUE));
            dataValue.addElement(random.nextInt(Integer.MAX_VALUE));
            dataValue.addElement(random.nextLong());
            dataValue.addElement(random.nextFloat());
            dataValue.addElement(random.nextDouble());
        } else {
            dataValue.addElement(-random.nextInt(Byte.MAX_VALUE));
            dataValue.addElement(-random.nextInt(Short.MAX_VALUE));
            dataValue.addElement(-random.nextInt(Integer.MAX_VALUE));
            dataValue.addElement(-random.nextLong());
            dataValue.addElement(-random.nextFloat());
            dataValue.addElement(-random.nextDouble());
        }
        return dataValue;
    }

    private static Vector<String> columnNames() {
        Vector<String> columnNames = new Vector<>();
        columnNames.addElement("boolean");
        columnNames.addElement("byte");
        columnNames.addElement("short integer");
        columnNames.addElement("integer");
        columnNames.addElement("long integer");
        columnNames.addElement("float");
        columnNames.addElement("double");
        return columnNames;
    }

    private static boolean flag() {
        return Math.random() >= 0.5;
    }
}
